package com.example.javaeekeshe.service.impl;

import com.example.javaeekeshe.mapper.GoodsCarMapper;
import com.example.javaeekeshe.pojo.GoodsAndGoodsCarBean;
import com.example.javaeekeshe.pojo.GoodsCarBean;
import com.example.javaeekeshe.service.IGoodsCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsCarServiceImpl implements IGoodsCarService {
    @Autowired
    GoodsCarMapper carMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)//事务控制
    public boolean addGoodsToCar(int uid, int gid, String color) {
        try {
            GoodsCarBean goodsCar = new GoodsCarBean();
            goodsCar.setUid(uid);
            goodsCar.setGid(gid);
            goodsCar.setCarcolor(color);
            carMapper.addCar(goodsCar);
            System.out.println("加入购物车成功");
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<GoodsAndGoodsCarBean> selectGoodsCarByUid(Integer uid) {
        return carMapper.selectGoodsCarByUid(uid);
    }

    @Override
    public boolean del(Integer id, Integer uid) {
        try {
            carMapper.delById(id,uid);
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public GoodsAndGoodsCarBean selectGoodCarByid(Integer id) {
        return carMapper.selectGoodsCarById(id);
    }
}
